/**
 * 
 */
package br.com.lazyprogrammer.worldsoccermanager.util;

import org.json.JSONObject;

import br.com.lazyprogrammer.worldsoccermanager.network.model.User;
import br.com.lazyprogrammer.worldsoccermanager.network.JSONConstants;
import br.com.lazyprogrammer.worldsoccermanager.network.model.Player;
import br.com.lazyprogrammer.worldsoccermanager.network.model.Stadium;
import br.com.lazyprogrammer.worldsoccermanager.network.model.Staff;
import br.com.lazyprogrammer.worldsoccermanager.network.model.Tactics;
import br.com.lazyprogrammer.worldsoccermanager.network.model.Team;

/**
 * 
 * @author Limao Games <br />
 * <b>Email:</b> andreikeda@limaogames.com.br <br />
 * created on: 07/05/2014
 * @version 1.0
 * @see 
 */
public class Parser {
	
	public Parser() {
		
	}
	
	public int parseResponse(JSONObject parent) {
		return (parent.optInt(JSONConstants.STATUS));
	}
	
	public Player parsePlayer(JSONObject jsonPlayer) {
		Player player	= null;
		player	= new Player(jsonPlayer.optString(JSONConstants.ID));
		player.setAge(jsonPlayer.optInt(JSONConstants.AGE));
		player.setAttributeAntecipation(jsonPlayer.optInt(JSONConstants.ATT_ANTECIPATION), jsonPlayer.optInt(JSONConstants.XP_ANTECIPATION));
		player.setAttributeConcentration(jsonPlayer.optInt(JSONConstants.ATT_CONCENTRATION), jsonPlayer.optInt(JSONConstants.XP_CONCENTRATION));
		player.setAttributeCreativity(jsonPlayer.optInt(JSONConstants.ATT_CREATIVY), jsonPlayer.optInt(JSONConstants.XP_CREATIVY));
		player.setAttributeFinishing(jsonPlayer.optInt(JSONConstants.ATT_FINISHING), jsonPlayer.optInt(JSONConstants.XP_FINISHING));
		player.setAttributeHandling(jsonPlayer.optInt(JSONConstants.ATT_HANDLING), jsonPlayer.optInt(JSONConstants.XP_HANDLING));
		player.setAttributeHappiness(jsonPlayer.optInt(JSONConstants.ATT_HAPPINESS), jsonPlayer.optInt(JSONConstants.XP_HAPPINESS));
		player.setAttributeHeading(jsonPlayer.optInt(JSONConstants.ATT_HEADING), jsonPlayer.optInt(JSONConstants.XP_HEADING));
		player.setAttributeLoyalty(jsonPlayer.optInt(JSONConstants.ATT_LOYALTY), jsonPlayer.optInt(JSONConstants.XP_LOYALTY));
		player.setAttributeMarking(jsonPlayer.optInt(JSONConstants.ATT_MARKING), jsonPlayer.optInt(JSONConstants.ATT_MARKING));
		player.setAttributePassing(jsonPlayer.optInt(JSONConstants.ATT_PASSING), jsonPlayer.optInt(JSONConstants.XP_PASSING));
		player.setAttributePositioning(jsonPlayer.optInt(JSONConstants.ATT_POSITIONING), jsonPlayer.optInt(JSONConstants.XP_POSITIONING));
		player.setAttributeReflexe(jsonPlayer.optInt(JSONConstants.ATT_REFLEXE), jsonPlayer.optInt(JSONConstants.XP_REFLEXE));
		player.setAttributeStrength(jsonPlayer.optInt(JSONConstants.ATT_STRENGTH), jsonPlayer.optInt(JSONConstants.XP_STRENGTH));
		player.setAttributeTackling(jsonPlayer.optInt(JSONConstants.ATT_TACKLING), jsonPlayer.optInt(JSONConstants.XP_TACKLING));
		player.setAttributeTechnique(jsonPlayer.optInt(JSONConstants.ATT_TECHNIQUE), jsonPlayer.optInt(JSONConstants.XP_TECHNIQUE));
		player.setContractValue(jsonPlayer.optInt(JSONConstants.CONTRACT_VALUE));
		player.setName(jsonPlayer.optString(JSONConstants.NAME));
		player.setNickname(jsonPlayer.optString(JSONConstants.NICKNAME));
		player.setPosition(jsonPlayer.optInt(JSONConstants.POSITION));
		player.setTeam(parseTeam(jsonPlayer.optJSONObject(JSONConstants.TEAM)));
		return player;
	}

	public Staff parseStaff(JSONObject jsonStaff) {
		Staff staff	= null;
		staff	= new Staff(jsonStaff.optString(JSONConstants.ID));
		staff.setContractValue(jsonStaff.optLong(JSONConstants.CONTRACT_VALUE));
		staff.setDetermination(jsonStaff.optInt(JSONConstants.DETERMINATION));
		staff.setDiscipline(jsonStaff.optInt(JSONConstants.DISCIPLINE));
		staff.setMotivation(jsonStaff.optInt(JSONConstants.MOTIVATION));
		staff.setName(jsonStaff.optString(JSONConstants.NAME));
		staff.setValue(jsonStaff.optLong(JSONConstants.VALUE));
		staff.setTactics(parseTactics(jsonStaff.optJSONObject(JSONConstants.TACTICS)));
		return staff;
	}

	public Tactics parseTactics(JSONObject jsonTactics) {
		Tactics tactics	= null;
		tactics	= new Tactics(jsonTactics.optString(JSONConstants.ID));
		tactics.setAdvancer(jsonTactics.optInt(JSONConstants.ADVANCER));
		tactics.setAdvMidfield(jsonTactics.optInt(JSONConstants.ADV_MIDFIELD));
		tactics.setDefender(jsonTactics.optInt(JSONConstants.DEFENDER));
		tactics.setMidfield(jsonTactics.optInt(JSONConstants.MIDFIELD));
		tactics.setMidDefender(jsonTactics.optInt(JSONConstants.MID_DEFENDER));
		return tactics;
	}

	public Team parseTeam(JSONObject jsonTeam) {
		Team team	= null;
		team	= new Team(jsonTeam.optString(JSONConstants.ID),
				jsonTeam.optString(JSONConstants.NAME),
				jsonTeam.optString(JSONConstants.LONGNAME));
		team.setCash(jsonTeam.optLong(JSONConstants.CASH));
		team.setDraw(jsonTeam.optInt(JSONConstants.DRAW));
		team.setEvaluation(jsonTeam.optLong(JSONConstants.EVALUATION));
		team.setLose(jsonTeam.optInt(JSONConstants.LOSE));
		team.setSupporter(jsonTeam.optLong(JSONConstants.SUPPORTER));
		team.setWin(jsonTeam.optInt(JSONConstants.WIN));
		team.setCreatedOn(jsonTeam.optLong(JSONConstants.CREATED_ON));
		team.setStadium(parseStadium(jsonTeam.optJSONObject(JSONConstants.STADIUM)));
		return team;
	}
	
	public Stadium parseStadium(JSONObject jsonStadium) {
		Stadium stadium	= null;
		stadium	= new Stadium(jsonStadium.optString(JSONConstants.ID),
				jsonStadium.optString(JSONConstants.NAME), 
				jsonStadium.optInt(JSONConstants.CAPACITY), 
				jsonStadium.optInt(JSONConstants.TICKET));
		return stadium;
	}
	
	public User parseUser(JSONObject jsonUser) {
		User user = null;
		user	= new User(jsonUser.optString(JSONConstants.ID));
		user.setUsername(jsonUser.optString(JSONConstants.USERNAME));
        user.setEmail(jsonUser.optString(JSONConstants.EMAIL));
		user.setActive(jsonUser.optInt(JSONConstants.ACTIVE));
		user.setCash(jsonUser.optLong(JSONConstants.LEMONASH));
		user.setDescription(jsonUser.optString(JSONConstants.DESCRIPTION));
		user.setKey(jsonUser.optString(JSONConstants.KEY));
		user.setOnline(jsonUser.optInt(JSONConstants.ONLINE));
		user.setPoints(jsonUser.optLong(JSONConstants.POINTS));
		user.setRegisteredDate(jsonUser.optLong(JSONConstants.REGISTERED_DATE));
		return user;
	}

}
